package com.xyz.gulimall.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.xyz.gulimall.product.entity.CategoryBrandRelationEntity;
import com.xyz.gulimall.product.service.CategoryBrandRelationService;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xyz.common.utils.PageUtils;
import com.xyz.common.utils.Query;

import com.xyz.gulimall.product.dao.BrandDao;
import com.xyz.gulimall.product.entity.BrandEntity;
import com.xyz.gulimall.product.service.BrandService;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;


@Service("brandService")
public class BrandServiceImpl extends ServiceImpl<BrandDao, BrandEntity> implements BrandService {

     @Resource
     CategoryBrandRelationServiceImpl categoryBrandRelationService;

     @Override
     public PageUtils queryPage(Map<String, Object> params) {
          String key = (String) params.get("key");
          QueryWrapper<BrandEntity> wrapper = new QueryWrapper<>();
          if(!StringUtils.isEmpty(key)){
               wrapper.eq("brand_id",key).or().like("name",key);
          }
          IPage<BrandEntity> page = this.page(new Query<BrandEntity>().getPage(params),wrapper);
              return new PageUtils(page);
     }


     @Override
     @Transactional(rollbackFor = Exception.class)
     public void updateDetail(BrandEntity brand) {
          //保证冗余字段的数据一致
          this.updateById(brand);
          if(!StringUtils.isEmpty(brand.getName())){
               //同步更新其他关联表中的数据
               categoryBrandRelationService.update(new UpdateWrapper<CategoryBrandRelationEntity>().set("brand_name",brand.getName()).eq("brand_id",brand.getBrandId()));
          }

     }

}